package cn.itzzx.lagou.mybatis.test;

import cn.itzzx.lagou.mybatis.dao.IUserDao;
import cn.itzzx.lagou.mybatis.io.Resources;
import cn.itzzx.lagou.mybatis.pojo.User;
import cn.itzzx.lagou.mybatis.session.SqlSession;
import cn.itzzx.lagou.mybatis.session.SqlSessionFactory;
import cn.itzzx.lagou.mybatis.session.SqlSessionFactoryBuilder;
import org.dom4j.DocumentException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.beans.IntrospectionException;
import java.beans.PropertyVetoException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;

/**
 * sqlMapConfig.xml 配置数据库地址
 *
 * @author Martin
 * @date 2020-04-25
 * @see 1.0
 */
public class MyBatis {


    /**
     * 传统方式
     */
    @Test
    public void test1() throws PropertyVetoException, DocumentException, SQLException, ClassNotFoundException, IntrospectionException, InstantiationException, IllegalAccessException, InvocationTargetException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory builder = new SqlSessionFactoryBuilder().builder(resourceAsStream);
        SqlSession sqlSession = builder.openSession();
        User user = new User();
        user.setId(2);
        user.setUsername("tom");
        //List<Object> objects = sqlSession.selectList("cn.itzzx.lagou.mybatis.dao.IUserDao.findByCondition",user);

        //sqlSession.update("cn.itzzx.lagou.mybatis.dao.IUserDao.findAll");

        List<Object> objects = sqlSession.selectList("cn.itzzx.lagou.mybatis.dao.IUserDao.findAll");
        System.out.println(objects);
    }


    /**
     * getMapper 方式
     */
    private IUserDao userDao;

    @Before
    public void before() throws PropertyVetoException, DocumentException {
        //将 sqlMapConfig.xml 加载内存当中，通过流的方式
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory builder = new SqlSessionFactoryBuilder().builder(resourceAsStream);
        SqlSession sqlSession = builder.openSession();
        userDao = sqlSession.getMapper(IUserDao.class);
        selectList();
    }

    @After
    public void after() {
        System.out.println("----------------------------------------");
        selectList();
    }


    public void selectList() {
        List<User> users = userDao.selectAll();
        for (User user : users) {
            System.out.println(user);
        }
    }

    @Test
    public void insertUser() {
        User user = new User();
        user.setUsername("Martin");
        user.setPassword("12355");
        user.setBirthday("2019-08-08");
        userDao.insertUser(user);
    }

    @Test
    public void updateUser() {
        User user = new User();
        user.setId(16);
        user.setUsername("Jack");
        user.setPassword("123456");
        user.setBirthday("1999-09-09");
        userDao.updateUser(user);
    }

    @Test
    public void deleteUser() {
        userDao.deleteById(16);
    }
}
